package com.campus.happy;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.campus.utils.Http;
import com.example.campus.R;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class HappyActivity extends Activity {
	String url = "http://api.meituan.com/meishi/filter/v3/deal/select/position/34.157494%2C108.919935/cate/17?sort=distance&mypos=34.157494%2C108.919935&distance=3000&hasGroup=true&ci=42&mpt_cate1=1&mpt_cate2=17&mpt_position=34.157494%2C108.919935&wifi-name=FAST_99A7B4%08wifi%08CMCC-FREEMM%08DeepDarkFantacy%08&wifi-mac=ec%3A88%3A8f%3A99%3Aa7%3Ab4%0864%3A27%3A37%3Af0%3Aa5%3A55%0806%3A08%3Ad2%3Ae6%3A09%3A31%0816%3Ae5%3A43%3Ad9%3Ac8%3A20%08&wifi-strength=-37%08-30%08-54%08-65%08&wifi-cur=0&offset=30&limit=15&poiFields=phone%2Caddr%2Cchannel%2CmarkNumbers%2CcityId%2ChasGroup%2Ccates%2CfrontImg%2CchooseSitting%2CavgPrice%2CavgScore%2Cname%2CcateId%2Clng%2Clat%2CshowType%2CcateName%2CareaName%2CzlSourceType%2CcampaignTag%2Cktv%2Crecommendation%2CiUrl%2CgroupInfo%2Cdiscount%2CisWaimai&dealFields=channel%2Cid%2Ccate%2Cdtype%2Cctype%2Csolds%2Cstatus%2Crange%2Cstart%2Cend%2Cimgurl%2Csquareimgurl%2Ctitle%2Cprice%2Cvalue%2Cbrandname%2Crating%2Crate-count%2Cnobooking%2ChotelExt%2Cshowtype%2Cdeposit%2Coptionalattrs%2Cdigestion&client=android&__vhost=api.meishi.meituan.com&utm_source=UC&utm_medium=android&utm_term=381&version_name=6.8.1&utm_content=863077022980290&utm_campaign=AgroupBgroupC1080993009273397760_a42204188_c10_d0_e6676741533979118973_f42204188E576592815841618432_e17520607876778740975_v181583745103389510__1_c1_a%25e8%25a5%25bf%25e5%25ae%2589%25e8%25b4%25a2%25e7%25bb%258f%25e5%25ad%25a6%25e9%2599%25a2%25e9%2595%25bf%25e5%25ae%2589%25e6%25a0%25a1%25e5%258c%25ba_b100002_t7017330778072583453Ghomepage_search&msid=8630770229802901462943089609&uuid=240D5678606145E1A6AF8A0E5EFD1584FE7CF27C6F1762868D6D19EDFC09FC49&userid=-1&__reqTraceID=2a9cfdf4-5d94-47a5-991d-f34ccd05fb0a&__skck=6a375bce8c66a0dc293860dfa83833ef&__skts=1462946627083&__skua=b610a5f9312fa0fabc5559648a0ff59e&__skno=e20c73f1-ccb8-44ff-b589-f5f93def9056&__skcy=9xIOiIwHDZlQlu%2BM9MhyO8Pt2rY%3D";
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 0x123) {

			}
		}

	};
	ArrayList<Info> list2;
	ListView listview;
	TabLayout k;
	private List<Fragment> mTabContents = new ArrayList<Fragment>();
	private NetListPagerAdapter mAdapter;
	private ViewPager mViewPager;
	private List<View> mDatas = new ArrayList<View>();
	private ViewPagerIndicator mIndicator;
	private HorizontalScrollView horizontalScrollView;
	private int tabWidth;
	private LinearLayout llLayout;
	private List<String> list = Arrays.asList("栏目一", "栏目二", "栏目三", "栏目四",
			"栏目五", "栏目六", "栏目七", "栏目八");
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.a);
		Load.cache=BitmapFactory.decodeResource(getResources(), R.drawable.fast_spread_disconnect_bkg_normal);
		mViewPager = (ViewPager) findViewById(R.id.vp);
		mViewPager.setOffscreenPageLimit(10);
		mIndicator = (ViewPagerIndicator) findViewById(R.id.indicator);
		llLayout = (LinearLayout) findViewById(R.id.ll1);
		horizontalScrollView = (HorizontalScrollView) findViewById(R.id.hsv);
		tabWidth =150;
		list=Arrays.asList(new String[] {"快餐","自助餐","咖啡","甜点","美发","电影院","景点","运动"});
		
		//this.getResources().getDimensionPixelOffset(10);
		for (int i = 0; i < list.size(); i++) {
			mDatas.add(new View(this));
			TextView textView = new TextView(this);
			textView.setText(list.get(i));
			textView.setGravity(Gravity.CENTER);
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					tabWidth, LinearLayout.LayoutParams.FILL_PARENT);
			llLayout.addView(textView,layoutParams);
		}String []s=new String[]{"http://www.dianping.com/search/category/17/10/g112r467p","http://www.dianping.com/search/category/17/10/g111r467p","http://www.dianping.com/search/category/17/10/g132r467p","http://www.dianping.com/search/category/17/10/g117r467p","http://www.dianping.com/search/category/17/50/g157p","http://www.dianping.com/search/category/17/25/g136p","http://www.dianping.com/search/category/17/35/r467p","http://www.dianping.com/search/category/17/45/r467p"};
		mAdapter = new NetListPagerAdapter(s, this);

		mIndicator.setChildViewWidth(tabWidth);
		mIndicator.setHorizontalScrollView(horizontalScrollView);
		mIndicator.setTabItemTitles(mDatas);
		// 设置关联的ViewPager
		mViewPager.setAdapter(mAdapter);
		mViewPager.setCurrentItem(0);
		mIndicator.setViewPager(mViewPager, 0);
		
		
//		//TabViewPager tab = new TabViewPager(this);
//
//		WindowManager wm = (WindowManager) getSystemService(
//				Context.WINDOW_SERVICE);
//
//		int width = wm.getDefaultDisplay().getWidth();
//		int height = wm.getDefaultDisplay().getHeight();
//String []s=new String[]{"http://www.dianping.com/search/category/17/10/g112r467p","http://www.dianping.com/search/category/17/10/g111r467p","http://www.dianping.com/search/category/17/10/g132r467p","http://www.dianping.com/search/category/17/10/g117r467p","http://www.dianping.com/search/category/17/50/g157p","http://www.dianping.com/search/category/17/25/g136p"};
//		//tab.initTabs(new String[] {"快餐","自助餐","咖啡","甜点","美发","电影院"}, width);
//	//	tab.setAdapter(new NetListPagerAdapter(s, this));
//		TabPager tab=(new TabPager(this));
//		String []ss=new String[] {"快餐","自助餐","咖啡","甜点","美发","电影院"};
//		//tab.vp.setAdapter(new NetListPagerAdapter(s, this));
//		LinearLayout l=new LinearLayout(this);
//		l.setOrientation(LinearLayout.VERTICAL);
//		ViewPagerIndicator i=new ViewPagerIndicator(this);
//		i.setDatas(Arrays.asList(ss));
//		ViewPager vp=new ViewPager(this);
//		vp.setAdapter(new NetListPagerAdapter(s, this));
//		LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
//		
//		i.setViewPager(vp);
//		l.addView(i,lp);
//		l.addView(vp);
//		setContentView(l);
//		
//		listview = new ListView(HappyActivity.this);
//		setContentView(listview);
//		try {
//			// test();
//		} catch (Exception e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}

		

		// new Thread() {
		// public void run() {
		// try {
		// try {
		// JSONObject o = new JSONObject(Http.get(url, "UTF-8"));
		// JSONArray data = o.getJSONArray("data");
		// for (int i = 0; i < data.length(); i++) {
		// JSONObject dataO = data.getJSONObject(i);
		//
		// }
		//
		// } catch (JSONException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		//
		// } catch (MalformedURLException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		//
		// byte[] b = null;
		// try {
		// b =
		// Http.get("http://p1.meituan.net/w.h/deal/505ad53e4757064afd4a01ff34229e8f281600.jpg");
		// } catch (MalformedURLException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// final Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
		// Handler h=new Handler();
		// h.post(new Runnable(){
		//
		// @Override
		// public void run() {
		// // TODO Auto-generated method stub
		// ImageView img=new ImageView(HappyActivity.this);
		// // /setContentView(img);
		// img.setImageBitmap(bitmap);
		// }
		//
		// });
		// }
		// }.start();

	}

	ArrayList<Info> create() {

		ArrayList<Info> info = new ArrayList();
		// Info info1=new Info(R.drawable.);
		// / info.add(info1);
		return info;

	}

	void test() throws Exception {
		File file = new File("sdcard/tessdata");
		if (!file.exists()) {
			file.mkdir();
		}

		InputStream myInput;
		OutputStream myOutput = new FileOutputStream(file);
		myInput = this.getAssets().open("tessdata/log.txt");
		byte[] buffer = new byte[512];
		int length = myInput.read(buffer);
		while (length > 0) {
			myOutput.write(buffer, 0, length);
			length = myInput.read(buffer);
		}

		myOutput.flush();
		myInput.close();
		myOutput.close();

	}
}
